﻿using System;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using HengTong.Model.Db;
using HengTong.Model.Db.System;


namespace HengTong.ViewModel._Admin.SysCodeHistoryVms
{
    public partial class SysCodeHistoryVm : BaseCRUDVM<SysCodeHistory>
    {

        public SysCodeHistoryVm()
        {
            SetInclude(x => x.CodeRule);
        }

        protected override void InitVM()
        {
        }

        public override async Task DoAddAsync()
        {
            await base.DoAddAsync();
        }

        public override async Task DoEditAsync(bool updateAllFields = false)
        {
            await base.DoEditAsync(updateAllFields);
        }
        
        public async Task ApproveAsync(bool upAllFields = false)
        {
            DC = Wtm.CreateDC();
            var transaction = DC.BeginTransaction();
            try
            {
                await base.DoEditAsync(upAllFields);
                if (MSD.IsValid)
                {
                    await ContinueWorkflowAsync(ActionName, Remark);
                    await transaction.CommitAsync();
                }
                else
                {
                    await transaction.RollbackAsync();
                }
            }
            catch (Exception e)
            {
                // 回滚事务
                await transaction.RollbackAsync();
                MSD.AddModelError(" ", $"审批操作异常: {e.Message}");
            }
            finally
            {
                await transaction.DisposeAsync();
            }
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}
